from concurrent.futures import ThreadPoolExecutor  # 线程池
import threading
from threading import Thread
import time
import queue
import requests
import json


class TestClass():
    def __init__(self,links, result_queue):


        # 定义10个线程的线程池用于请求数据
        self.thread_pool1 = ThreadPoolExecutor(max_workers=20)

        #链接列表
        self.links = links

        self.result_queue= result_queue

    def main_logic(self):

        for link in self.links:
            self.thread_pool1.submit(self.do_request, link)


    def do_request(self, link):

        data = requests.get(link)
        body = json.loads(data.text)

        if isinstance(body,dict):
        #学校名称
            name = body["data"]["name"]
        # 学校id
            school_id=body["data"]["school_id"]
        #省份
            province_name=body["data"]["province_name"]
        # 硕士点个数
            num_master=body["data"]["num_master"]
        # 博士点个数
            num_doctor=body["data"]["num_doctor"]
        # 国家特色学科个数
            num_subject=body["data"]["num_subject"]
        # 实验室个数
            num_lab=body["data"]["num_lab"]

            content=province_name+" "+name+" "+school_id+" "+num_master+" "+num_doctor+" "+num_subject+" "+num_lab+"\n"

            self.result_queue.put(content)




def write_to_file(q,file):
    while q.empty() != True:
        file.write(q.get())



if __name__ == "__main__":

    print(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()))
    print("loading........")


    front_links = ["https://static-data.eol.cn/www/2.0/school/{}/info.json".format(i) for i in range(30, 623)]
    end_links = ["https://static-data.eol.cn/www/2.0/school/{}/info.json".format(i) for i in range(624, 3700)]
    all_links = front_links + end_links

    q = queue.Queue()

    file=open("a.txt","a+")


    obj = TestClass(all_links, q)

    obj.main_logic()
    obj.thread_pool1.shutdown()


    # 写线程
    t = Thread(target=write_to_file, args=(q, file))

    t.start()
    t.join()

    file.close()

    print(time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()))


